package com.lovecanon.jzjl.web.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lovecanon.jzjl.common.BaseService;
import com.lovecanon.jzjl.common.BtPage;
import com.lovecanon.jzjl.common.ResultData;
import com.lovecanon.jzjl.domain.TbReceive;
import com.lovecanon.jzjl.mapper.TbReceiveMapper;
import com.lovecanon.jzjl.repository.ReceiveRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class ReceiveService extends BaseService {


    @Autowired
    private TbReceiveMapper tbReceiveMapper;

    @Autowired
    private ReceiveRepository receiveRepository;

    public List<TbReceive> findAll(){
        return  receiveRepository.findAll();
    }

    public ResultData saveOrUpdate(TbReceive tbReceive){
        ResultData resultData = new ResultData();
        try {
            if (!StringUtils.isEmpty(tbReceive.getId())){
                if (tbReceive!=null){
                    receiveRepository.save(tbReceive);
                }
            }else{
                tbReceive.setCreateTime(new Date());
                receiveRepository.save(tbReceive);
            }
            return  resultData.requestSuccess();
        }catch (Exception e){
            e.printStackTrace();
            return  resultData.requestError();
        }
    }

    public TbReceive findOne(long id){

        try {
            TbReceive tbReceive = (TbReceive)find(TbReceive.class, id);
            return tbReceive;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public void deleByIds(String[] ids){
        for (int i = 0; i < ids.length; i++) {
            TbReceive tbReceive = receiveRepository.getOne(Long.valueOf(ids[i]));
            tbReceive.setIsUse("N");
            receiveRepository.save(tbReceive);
        }
    }
    /*
     * 这个方法中用到了我们开头配置依赖的分页插件pagehelper
     * 很简单，只需要在service层传入参数，然后将参数传递给一个插件的一个静态方法即可；
     * pageNum 开始页数
     * pageSize 每页显示的数据条数
     * */
    public BtPage<List<TbReceive>> findAll(Map<String,Object> map){
        int pageNum = (int)map.get("pageNumber");
        int pageSize =  (int)map.get("pageSize");
        BtPage<List<TbReceive>> listBtPage = new BtPage<>();
        PageHelper.startPage(pageNum, pageSize);
        List<TbReceive> tbReceives  = tbReceiveMapper.findAllByFileNameLike(map);
        PageInfo<TbReceive> pageInfo = new PageInfo<>(tbReceives);
        listBtPage.setCount((int) pageInfo.getTotal());
        listBtPage.setData(pageInfo.getList());
        return listBtPage;
    }

    public TbReceive getTbreceiveByFileName(String fileName){
        return  receiveRepository.findByFileNameAndIsUseNot(fileName,"N");
    }


}
